﻿package com.cilico.view;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

import com.cilico.R;
import com.cilico.components.ProgressDialog;
import com.cilico.utils.Utils;

/**
 * 闪屏背景图
 * @author lv.xiangan
 *
 */
public class SplashScreenBackgroundView extends TextView implements Runnable {
	private int processingCtr_ = 0;
	int screenW_;
	int screenH_;
	Paint paint_;
	private int ctr_;
	private Thread thread_;
	public static boolean stopped_;
	int bgColor_ = 0x00055db7;

	private final String LOAD = "获取中，请稍候...";
	private int degree_;

	public SplashScreenBackgroundView(Context context) {
		super(context);
		// 初始化进度提示框的旋转图片
		if (null == ProgressDialog.Progress.PROGRESSP) {
			ProgressDialog.Progress.PROGRESSP = Utils.getScaledBitmap(context.getResources(), R.drawable.progress);
		}
		if (null == ProgressDialog.Progress.MAT)
			ProgressDialog.Progress.MAT = new Matrix();
		stopped_ = false;
		paint_ = new Paint();
		screenW_ = Utils.screenWidth_;
		screenH_ = Utils.screenHeight_;
		setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		thread_ = new Thread(this);
		thread_.start();
	}

	public void onDraw(Canvas canvas) {
		canvas.drawColor(bgColor_);
		Bitmap bmpLoadingRun_ = ProgressDialog.Progress.PROGRESSP;
		final int x = (screenW_ - bmpLoadingRun_.getWidth()) >> 1;
		final int y = (int) ((float) (350 / 420f) * screenH_);
		final int yBmp = (int) (y - paint_.getTextSize() - (bmpLoadingRun_.getHeight() << 1));
		// 设置转圈的角度和中心点
		ProgressDialog.Progress.MAT.setRotate(degree_, bmpLoadingRun_.getWidth() >> 1, bmpLoadingRun_.getHeight() >> 1);
		ProgressDialog.Progress.MAT.postTranslate(x, yBmp);
		degree_ = degree_ + 45;
		if (degree_ == 360)
			degree_ = 0;
		canvas.drawBitmap(bmpLoadingRun_, ProgressDialog.Progress.MAT, paint_);
		// centerX_ = screenW_ / 2;
		// logoY_ = 3 * screenH_ / 5;
		paint_.setColor(0xff8B3E2F);
		paint_.setTextSize(Utils.getScaledValue(15));
		canvas.drawText(LOAD, (screenW_ - (int) paint_.measureText(LOAD)) >> 1, (float) (327 / 420f) * screenH_+15, paint_);
		// drawProcessing(canvas, centerX_ - LPUtils.progress_.getWidth() / 2, logoY_);

	}

	void drawProcessing(Canvas g, int centerX, int centerY) {
		Bitmap img = Utils.progress_;
		g.clipRect(centerX, centerY, centerX + 50, centerY + 5);
		g.drawBitmap(img, centerX, centerY, paint_);
		g.clipRect(new Rect(centerX + processingCtr_ * 9, centerY, centerX + processingCtr_ * 9 + 50 - processingCtr_ * 9, centerY + 5));
		g.drawBitmap(img, centerX + processingCtr_ * 9, centerY, paint_);
		g.clipRect(centerX, centerY, centerX + 50, centerY + 5);
		g.drawBitmap(img, centerX - (54 - processingCtr_ * 9), centerY, paint_);
		g.clipRect(0, 0, Utils.screenWidth_, Utils.screenHeight_);
	}

	public void run() {
		while (true) {
			if (stopped_) {
				return;
			}
			try {
				Thread.sleep(200);
			} catch (InterruptedException ex) {
			}
			onUpdate();
		}

	}

	public void onUpdate() {
		if (ctr_ == 0) {
			ctr_ = 1;
		} else if (ctr_ == 1) {
			ctr_ = 2;
		} else {
			processingCtr_ = ctr_ - 2;
			if (++ctr_ > 7) {
				ctr_ = 2;
			}
		}
		// 刷新界面执行onDraw方法，类似于j2me的repaint()
		postInvalidate();
	}
}
